package com.appbee.cm.client.startup;

import java.util.List;

import javax.inject.Inject;

import com.appbee.cm.client.nav.presenter.NavControl;
import com.appbee.cm.client.regionunit.presenter.ActivityEntryControl;
import com.appbee.cm.shared.data.ActivityEntryProxy;
import com.appbee.cm.shared.data.NavItemProxy;
import com.appbee.core.client.startup.StartupChain;
import com.appbee.core.client.startup.StartupLink;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.HasWidgets.ForIsWidget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;

public class CMStarupLink implements StartupLink
{
	private final NavControl navControl;
	private final ActivityEntryControl activityEntryControl;
	private final CMStartupLoader dataRequest;

	@Inject
	public CMStarupLink(NavControl navControl,
						ActivityEntryControl activityEntryControl,
						CMStartupLoader dataRequest)
	{
		this.navControl = navControl;
		this.activityEntryControl = activityEntryControl;
		this.dataRequest = dataRequest;
	}

	@Override
	public void start(ForIsWidget container, StartupChain chain)
	{
		// Nav request
		Request<List<NavItemProxy>> navRequest = dataRequest.findNavItems();
		navRequest.to(new Receiver<List<NavItemProxy>>()
		{

			@Override
			public void onSuccess(List<NavItemProxy> response)
			{
				GWT.log("navRequest done, size: " + response.size());
				navControl.setNavItems(response);
			}

		});

		// Activity request
		Request<List<ActivityEntryProxy>> activityEntriesRequest = dataRequest
				.findActivityEntries();
		activityEntriesRequest.to(new Receiver<List<ActivityEntryProxy>>()
		{
			@Override
			public void onSuccess(List<ActivityEntryProxy> response)
			{
				GWT.log("activityRequest done");
				activityEntryControl.setActivityEntries(response);
			}
		});
		chain.start(container);
	}

}
